package com.k2data.k2app.controller;

import com.k2data.k2app.domain.SysMail;
import com.k2data.k2app.mapper.SysMailMapper;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.SysMailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短信网管配
 * Created by clb on 17-3-17.
 */
@Api(description = "短信网管配置")
@RestController
@RequestMapping("/mail")
public class SysMailController {
	private final SysMailMapper sysMailMapper;
	private final SysMailService sysMailService;

	@Autowired
	public SysMailController(SysMailMapper sysMailMapper, SysMailService sysMailService) {
		this.sysMailMapper = sysMailMapper;
		this.sysMailService = sysMailService;
	}

	@ApiOperation(value = "创建")
	@ApiImplicitParam(name = "sysMail", value = "sysMail对象", required = true, dataType = "SysMail", paramType = "body")
	@PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<CommonResultResponse> post(@RequestBody SysMail sysSms) throws Exception {
		return ResponseHelper.success(sysMailService.insert(sysSms));
	}

	@ApiOperation(value = "删除")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
	@DeleteMapping(value = "/{id}")
	public ResponseEntity<CommonResultResponse> delete(@PathVariable("id") String id) throws Exception {
		return ResponseHelper.success(sysMailMapper.deleteById(Long.parseLong(id)));
	}

	@ApiOperation(value = "更新")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
			@ApiImplicitParam(name = "sysMail", value = "sysMail对象", required = true, dataType = "SysMail", paramType = "body")
	})
	@PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<CommonResultResponse> update(@PathVariable("id") Long id,
													   @RequestBody SysMail sysSms) throws Exception {
		sysSms.setId(id);
		return ResponseHelper.success(sysMailService.update(sysSms));
	}

	@ApiOperation(value = "根据id获取")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "/{id}")
	public ResponseEntity<CommonResultResponse> get(@PathVariable("id") Long id) throws Exception {
		return ResponseHelper.success(sysMailService.get(id));
	}

	@ApiOperation(value = "获取所有记录")
	@GetMapping(value = "")
	public ResponseEntity<CommonResultResponse> selectAll() throws Exception {
		return ResponseHelper.success(sysMailMapper.selectAll());
	}

	@ApiOperation(value = "获取有效记录")
	@GetMapping(value = "/useful")
	public ResponseEntity<CommonResultResponse> selectUseful() throws Exception {
		List<SysMail> list = sysMailMapper.selectUseful();
		if(list.size()>0){
			return ResponseHelper.success(list.get(0));
		}else{
			return ResponseHelper.success(new SysMail());
		}
	}
}
